import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../spaces/filespace/fileSpace_ftpfile.dart';
import '../../helper/Helper.dart';
import '../../helper/menuHelper.dart';
import '../../helper/routeHelper.dart';
import '../../model/filespace.dart';
import '../../widgets/inputWidget.dart';
import '../../widgets/menu_item.dart';

import 'fileSpace.dart';

class FileSpaceListPage extends StatefulWidget {
  @override
  FileSpaceListPage();

  @override
  FileSpaceListPageState createState() => new FileSpaceListPageState();
}

class FileSpaceListPageState extends State<FileSpaceListPage> {
  final Icon icon1 = Icon(
    Icons.folder,
    size: 48.0,
    color: Color.fromRGBO(218, 165, 32, 100),
  );
  late List<FileSpace> fList;
  late List<String> tList;

  bool ifLoaded = false;
  @override
  void initState() {
    // TODO: implement initState

    super.initState();
    //  getML();
    loadData();
  }

  void loadData() async {
    fList = await FileSpace.GetAllList();
    tList = await FileSpace.GetTypeList();

    print(tList);
    // print(Helper.DateTimeFormat( fList[0].CreateTM));
    //var y1=fList[0];
    // y1.ID=0;
    //FileSpace.Add(y1);
    setState(() {
      ifLoaded = true;
    });

    //ChannelHelper.showToast("更新完成!");
  }

  MenuItemViewModel getItem(FileSpace f1) {
    return MenuItemViewModel(
        icon1,
        f1.Name,
        () => RouteHelper.ShowPage(
            context,
            FileSpaceFtpFilePage(
              f1.Guid,
              f1.Name,
              true,
              ifAppbar: true,
            )),
        col1: Colors.white);
  }

  Widget getPanel2(String c1) {
    var mList = <Widget>[];
    //mList=fList.map((f1)=> f1.Category==c1?MenuItem2(data: getItem(f1)):new Container()).toList();
    for (var f1 in fList) {
      if (f1.Category == c1) {
        mList.add(MenuItem2(data: getItem(f1)));
      }
    }
    return MenuHelper.getMenuCard(c1, mList);
  }

  Widget getPanel(String c1) {
    var mList = <Widget>[];
    // mList=fList.map((f1)=> f1.Category==c1?MenuItem2(data: getItem(f1)):new Container()).toList();
    for (var f1 in fList) {
      if (f1.Category == c1) {
        mList.add(MenuItem2(data: getItem(f1)));
      }
    }
    var list1 = <Widget>[];
    list1.add(InputWidget.getGreyInfo2(c1, ""));

    list1.add(MenuHelper.getMenuRowList(mList, 4));
    list1.add(InputWidget.getDivider());
    return InputWidget.inCard3(list1, margin1: 0.0);
  }

  Icon getIcon(IconData d1) {
    return Icon(d1, size: 48.0, color: Colors.yellow);
  }

  @override
  Widget build(BuildContext context) {
    //getML(context);
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(
          // title: bottomTabs[currentIndex].title,
          title: Text("空间列表")),
      floatingActionButton: getFloatingButton(context),
      body: FileSpaceListBody(context),
    );
  }

  Widget getFloatingButton(BuildContext context) {
    return new Container();
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    for (var item in tList) {
      list1.add(getPanel(item));
    }
    //list1.add(getPanel("水库调度"));
    //list1.add(getPanel("水库调度"));
    //list1.add(getPanel("水库调度"));
    //list1.add(getPanel("水库调度"));
    //list1.add(getPanel("水库调度"));

    return list1;
  }

  FileSpaceListBody(BuildContext context) => SingleChildScrollView(
        child: FileSpaceListFields(context),
      );

  FileSpaceListFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
